{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solving Problems With Dynamic Programming"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Dynamic programming is a really useful general technique for solving problems that involves breaking down problems into smaller overlapping sub-problems, storing the results computed from the sub-problems and reusing those results on larger chunks of the problem.  Dynamic programming solutions are pretty much always more efficent than naive brute-force solutions.  Dynamic programming techniques are particularly effective on problems that contain [optimal substructure](https://en.wikipedia.org/wiki/Optimal_substructure).\n",
    "\n",
    "Dynamic programming is related to a number of other fundamental concepts in computer science in interesting ways.  Recursion, for example, is similar to (but not identical to) dynamic programming.  The key difference is that in a naive recursive solution, answers to sub-problems may be computed many times.  A recursive solution that caches answers to sub-problems which were already computed is called [memoization](https://en.wikipedia.org/wiki/Memoization), which is basically the inverse of dynamic programming.  Another variation is when the sub-problems don't actually overlap at all, in which case the technique is known as [divide and conquer](https://en.wikipedia.org/wiki/Divide_and_conquer_algorithms).  Finally, dynamic programming is tied to the concept of [mathematical induction](https://en.wikipedia.org/wiki/Mathematical_induction) and can be thought of as a specific application of inductive reasoning in practice.\n",
    "\n",
    "While the core ideas behind dynamic programming are actually pretty simple, it turns out that it's fairly challenging to use on non-trivial problems because it's often not obvious how to frame a difficult problem in terms of overlapping sub-problems.  This is where experience and practice come in handy, which is the idea for this notebook.  We'll build both naive and \"intelligent\" solutions to several well-known problems and see how the problems are decomposed to use dynamic programming solutions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fibonacci Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we'll look at the problem of computing numbers in the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number).  The problem definition is very simple - each number in the sequence is the sum of the two previous numbers in the sequence.  Or, more formally:\n",
    "\n",
    "$F_n = F_{n-1} + F_{n-2}$, with $F_0 = 0$ and $F_1 = 1$ as the seed values.\n",
    "\n",
    "Our solution will be responsible for calculating each of Fibonacci numbers up to some defined limit.  We'll first implement a naive solution that re-calculates each number in the sequence from scratch."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    if n == 0:\n",
    "        return 0\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    \n",
    "    return fib(n - 1) + fib(n - 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def all_fib(n):\n",
    "    fibs = []\n",
    "    for i in range(n):\n",
    "        fibs.append(fib(i))\n",
    "    \n",
    "    return fibs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try it out on a pretty small number first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time print(all_fib(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, probably too trivial.  Let's try a bit bigger..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]\n",
      "Wall time: 5 ms\n"
     ]
    }
   ],
   "source": [
    "%time print(all_fib(20))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The runtime was at least measurable now, but still pretty quick.  Let's try one more time..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]\n",
      "Wall time: 1min 9s\n"
     ]
    }
   ],
   "source": [
    "%time print(all_fib(40))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That escalated quickly!  Clearly this is a pretty bad solution.  Let's see what it looks like when applying dynamic programming."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def all_fib_dp(n):\n",
    "    fibs = []\n",
    "    for i in range(n):\n",
    "        if i < 2:\n",
    "            fibs.append(i)\n",
    "        else:\n",
    "            fibs.append(fibs[i - 2] + fibs[i - 1])\n",
    "    \n",
    "    return fibs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This time we're saving the result at each iteration and computing new numbers as a sum of the previously saved results.  Let's see what this does to the performance of the function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time print(all_fib_dp(40))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By not computing the full recusrive tree on each iteration, we've essentially reduced the running time for the first 40 numbers from ~75 seconds to virtually instant.  This also happens to be a good example of the danger of naive recursive functions.  Our new Fibonaci number function can compute additional values in linear time vs. exponential time for the first version."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073L, 4807526976L, 7778742049L, 12586269025L, 20365011074L, 32951280099L, 53316291173L, 86267571272L, 139583862445L, 225851433717L, 365435296162L, 591286729879L, 956722026041L, 1548008755920L, 2504730781961L, 4052739537881L, 6557470319842L, 10610209857723L, 17167680177565L, 27777890035288L, 44945570212853L, 72723460248141L, 117669030460994L, 190392490709135L, 308061521170129L, 498454011879264L, 806515533049393L, 1304969544928657L, 2111485077978050L, 3416454622906707L, 5527939700884757L, 8944394323791464L, 14472334024676221L, 23416728348467685L, 37889062373143906L, 61305790721611591L, 99194853094755497L, 160500643816367088L, 259695496911122585L, 420196140727489673L, 679891637638612258L, 1100087778366101931L, 1779979416004714189L, 2880067194370816120L, 4660046610375530309L, 7540113804746346429L, 12200160415121876738L, 19740274219868223167L, 31940434634990099905L, 51680708854858323072L, 83621143489848422977L, 135301852344706746049L, 218922995834555169026L]\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time print(all_fib_dp(100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Longest Increasing Subsequence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Fibonacci problem is a good starter example but doesn't really capture the challenge of representing problems in terms of optimal sub-problems because for Fibonacci numbers the answer is pretty obvious.  Let's move up one step in difficulty to a problem known as the [longest increasing subsequence](https://en.wikipedia.org/wiki/Longest_increasing_subsequence) problem.  The objective is to find the longest subsequence of a given sequence such that all elements in the subsequence are sorted in increasing order.  Note that the elements do not need to be contiguous; that is, they are not required to appear next to each other.  For example, in the sequence [ 10, 22, 9, 33, 21, 50, 41, 60, 80 ] the longest increasing subsequence (LIS) is [10, 22, 33, 50, 60, 80].\n",
    "\n",
    "It turns out that it's fairly difficult to do a \"brute-force\" solution to this problem.  The dynamic programming solution is much more concise and a natural fit for the problem definition, so we'll skip creating an unnecessarily complicated naive solution and jump straight to the DP solution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def find_lis(seq):\n",
    "    n = len(seq)\n",
    "    max_length = 1\n",
    "    best_seq_end = -1\n",
    "    \n",
    "    # keep a chain of the values of the lis\n",
    "    prev = [0 for i in range(n)]\n",
    "    prev[0] = -1\n",
    "    \n",
    "    # the length of the lis at each position\n",
    "    length = [0 for i in range(n)]\n",
    "    length[0] = 1\n",
    "    \n",
    "    for i in range(1, n):\n",
    "        length[i] = 0\n",
    "        prev[i] = -1\n",
    "        \n",
    "        # start from index i-1 and work back to 0\n",
    "        for j in range(i - 1, -1, -1):\n",
    "            if (length[j] + 1) > length[i] and seq[j] < seq[i]:\n",
    "                # there's a number before position i that increases the lis at i\n",
    "                length[i] = length[j] + 1\n",
    "                prev[i] = j\n",
    "        \n",
    "        if length[i] > max_length:\n",
    "            max_length = length[i]\n",
    "            best_seq_end = i\n",
    "    \n",
    "    # recover the subsequence\n",
    "    lis = []\n",
    "    element = best_seq_end\n",
    "    while element != -1:\n",
    "        lis.append(seq[element])\n",
    "        element = prev[element]\n",
    "    \n",
    "    return lis[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The intuition here is that for a given index $i$, we can compute the length of the longest increasing subsequence $length(i)$ by looking at all indices $j < i$ and if $length(j) + 1 > i$ and $seq[j] < seq[i]$ (meaning there's a number at position $j$ that increases the longest subsequence at that index such that it is now longer than the longest recorded subsequence at $i$) then we increase $length(i)$ by 1.  It's a bit confusing at first glance but step through it carefully and convince yourself that this solution finds the optimal subsequence.  The \"prev\" list holds the indices of the elements that form the actual values in the subsequence.\n",
    "\n",
    "Let's generate some test data and try it out."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[16, 10, 17, 18, 9, 0, 2, 19, 4, 3, 1, 14, 12, 6, 2, 4, 11, 5, 19, 4]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "seq_small = list(np.random.randint(0, 20, 20))\n",
    "seq_small"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 4, 5, 19]\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time print(find_lis(seq_small))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just based on the eye test the output looks correct.  Let's see how well it performs on much larger sequences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[29, 94, 125, 159, 262, 271, 274, 345, 375, 421, 524, 536, 668, 689, 694, 755, 763, 774, 788, 854, 916, 1018, 1022, 1098, 1136, 1154, 1172, 1237, 1325, 1361, 1400, 1401, 1406, 1450, 1498, 1633, 1693, 1745, 1765, 1793, 1835, 1949, 1997, 2069, 2072, 2096, 2157, 2336, 2345, 2468, 2519, 2529, 2624, 2630, 2924, 3103, 3291, 3321, 3380, 3546, 3635, 3657, 3668, 3703, 3775, 3836, 3850, 3961, 4002, 4004, 4039, 4060, 4128, 4361, 4377, 4424, 4432, 4460, 4465, 4493, 4540, 4595, 4693, 4732, 4735, 4766, 4831, 4850, 4873, 4908, 4940, 4969, 5013, 5073, 5087, 5139, 5144, 5271, 5280, 5299, 5300, 5355, 5393, 5430, 5536, 5538, 5559, 5565, 5822, 5891, 5895, 5906, 6157, 6199, 6286, 6369, 6431, 6450, 6510, 6533, 6577, 6585, 6683, 6701, 6740, 6745, 6829, 6853, 6863, 6872, 6884, 6923, 6925, 7009, 7019, 7028, 7040, 7170, 7235, 7304, 7356, 7377, 7416, 7490, 7495, 7662, 7676, 7703, 7808, 7925, 7971, 8036, 8073, 8282, 8295, 8332, 8342, 8360, 8429, 8454, 8499, 8557, 8585, 8639, 8649, 8725, 8759, 8831, 8860, 8899, 8969, 9046, 9146, 9161, 9245, 9270, 9374, 9451, 9465, 9515, 9522, 9525, 9527, 9664, 9770, 9781, 9787, 9914, 9993]\n",
      "Wall time: 4.94 s\n"
     ]
    }
   ],
   "source": [
    "seq = list(np.random.randint(0, 10000, 10000))\n",
    "%time print(find_lis(seq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So it's still pretty fast, but the difference is definitely noticable.  At 10,000 integers in the sequence our algorithm already takes several seconds to complete.  In fact, even though this solution uses dynamic programming its runtime is still $O(n^2)$.  The lesson here is that dynamic programming doesn't always result in lightning-fast solutions.  There are also different ways to apply DP to the same problem.  In fact there's a solution to this problem that uses binary search trees and runs in $O(nlogn)$ time, significantly better than the solution we just came up with."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Knapsack Problem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem) is another classic dynamic programming exercise.  The generalization of this problem is very old and comes in many variations, and there are actually multiple ways to tackle this problem aside from dynamic programming.  Still, it's a common example for DP exercises.\n",
    "\n",
    "The problem at its core is one of combinatorial optimization.  Given a set of items, each with a mass and a value, determine the collection of items that results in the highest possible value while not exceeding some limit on the total weight.  The variation we'll look at is commonly referred to as the 0-1 knapsack problem, which restricts the number of copies of each kind of item to 0 or 1.  More formally, given a set of $n$ items each with weight $w_i$ and value $v_i$ along with a maximum total weight $W$, our objective is:\n",
    "\n",
    "$\\Large max \\Sigma v_i x_i$, where $\\Large \\Sigma w_i x_i \\leq W$\n",
    "\n",
    "Let's see what the implementation looks like then discuss why it works."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def knapsack(W, w, v):\n",
    "    # create a W x n solution matrix to store the sub-problem results\n",
    "    n = len(v)\n",
    "    S = [[0 for x in range(W)] for k in range(n)]\n",
    "    \n",
    "    for x in range(1, W):\n",
    "        for k in range(1, n):\n",
    "            # using this notation k is the number of items in the solution and x is the max weight of the solution,\n",
    "            # so the initial assumption is that the optimal solution with k items at weight x is at least as good\n",
    "            # as the optimal solution with k-1 items for the same max weight\n",
    "            S[k][x] = S[k-1][x]\n",
    "            \n",
    "            # if the current item weighs less than the max weight and the optimal solution including this item is \n",
    "            # better than the current optimum, the new optimum is the one resulting from including the current item\n",
    "            if w[k] < x and S[k-1][x-w[k]] + v[k] > S[k][x]:\n",
    "                S[k][x] = S[k-1][x-w[k]] + v[k]\n",
    "    \n",
    "    return S"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The intuition behind this algorithm is that once you've solved for the optimal combination of items at some weight $x < W$ and with some number of items $k < n$, then it's easy to solve the problem with one more item or one higher max weight because you can just check to see if the solution obtained by incorporating that item is better than the best solution you've already found.  So how do you get the initial solution?  Keep going down the rabbit hole until to reach 0 (in which case the answer is 0).  At first glance it's very hard to grasp, but that's part of the magic of dynamic programming.  Let's run an example to see what it looks like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([3, 9, 3, 6, 5], [40, 45, 72, 77, 16])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w = list(np.random.randint(0, 10, 5))\n",
    "v = list(np.random.randint(0, 100, 5))\n",
    "w, v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 45, 45, 45, 45],\n",
       " [0, 0, 0, 0, 72, 72, 72, 72, 72, 72, 72, 72, 72, 117, 117],\n",
       " [0, 0, 0, 0, 72, 72, 72, 77, 77, 77, 149, 149, 149, 149, 149],\n",
       " [0, 0, 0, 0, 72, 72, 72, 77, 77, 88, 149, 149, 149, 149, 149]]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knapsack(15, w, v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The output here is the array of optimal values for a given max weight (think of it as the column index) and max number of items (the row index).  Notice how the output follows what looks sort of like a wavefront pattern.  This seems to be a recurring phenomenon with dynamic programming solutions.  The value in the lower right corner is the max value that we were looking for under the given constraints and is the answer to the problem."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
